<script setup lang="ts">
import { home } from "@/api/home.ts";
import { onMounted, reactive, ref } from "vue";
import { ElMessage } from 'element-plus';

onMounted(() => {
  getFiles('E:\\Note\\pending');
});

const directory_path = ref('E:\\Note\\video');
const filePathList = ref<{ name: string }[]>([]);

const getFiles = (filePath: string) => {
  home.getFiles(filePath).then((res) => {
    filePathList.value = res.data.files.map((file: any) => ({
      name: file
    }));
  });
};

interface User {
  name: string
}

const multipleTableRef = ref()
const multipleSelection = ref<string[]>([])
const toggleSelection = () => {
  multipleTableRef.value!.clearSelection()
}

const handleSelectionChange = (val: User[]) => {
  multipleSelection.value = val.map((item: any) => item.name);
};

const mergeVideosButton = ()=>{
  if (multipleSelection.value.length === 0){
    ElMessage.error('请选择要合并的视频文件！');
    return
  }
  mergeVideosDialogItem.value = true;
}

const mergeVideosDialogItem = ref()
const mergeVideosDialog = ref()
const form = reactive({
  output_path: '',
})

// 添加表单校验规则
const rules = reactive({
  output_path: [
    { required: true, message: '视频输出路径不能为空！', trigger: 'blur' },
  ]
})

const loading = ref(false);
const mergeVideos = () => {
  // 检查 multipleSelection 是否为空
  if (multipleSelection.value.length === 0) {
    ElMessage.error('请选择要合并的视频文件！');
    return;
  }

  // 使用 el-form 的校验方法
  mergeVideosDialog.value.validate((valid: boolean) => {
    if (!valid) {
      return;
    }
    loading.value = true; // 设置 loading 为 true
    // 如果校验通过，执行合并视频操作
    home.getMerger(directory_path.value, [...new Set(multipleSelection.value.flat())], form.output_path).then((res) => {
      console.log(res);
      ElMessage.success('合并视频成功！');
      mergeVideosDialogItem.value = false;
      toggleSelection();
      // 清空表单数据
      mergeVideosDialog.value.resetFields();
    }).finally(() => {
      loading.value = false;
    });
  });
};


</script>

<template>
  <div>
    <el-row class="rounded-box">
      <el-col :span="24">
        <el-form-item label="视频路径">
          <el-input v-model="directory_path" autocomplete="off" placeholder="请输入路径，例如：E:\Note" style="width: 300px;margin-right: 20px"/>
          <el-button type="primary" @click="getFiles(directory_path)">确定</el-button>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row class="rounded-box">
      <el-col :span="24">
        <el-table ref="multipleTableRef" @selection-change="handleSelectionChange" :data="filePathList" style="width: 100%">
          <el-table-column type="selection" width="55" />
          <el-table-column  label="选中顺序序号" width="200">
            <template #default="scope">
              {{ multipleSelection.indexOf(scope.row.name) + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="name" label="文件名" width="180"/>
        </el-table>
        <div style="margin-top: 20px">
          <el-button @click="mergeVideosButton">合并视频</el-button>
        </div>
      </el-col>
    </el-row>

    <el-dialog v-model="mergeVideosDialogItem" title="是否需要合并视频？">
      <el-form v-loading="loading" element-loading-text="合并中..." :model="form" :rules="rules" ref="mergeVideosDialog">
        <el-form-item label="视频输出路径" prop="output_path">
          <el-input v-model="form.output_path" autocomplete="off" placeholder="请输入输出路径，例如：E:\Note"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="mergeVideosDialog = false">取消</el-button>
          <el-button type="primary" @click="mergeVideos">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.rounded-box {
  background-color: white;
  margin: 10px;
  padding: 20px;
  border-radius: 10px;
}
</style>